package com.zsrt.md5;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * 可以多次加密md5，即多次调用getMD5();
 * @author scott_wrl
 *
 */
public class MD5Util {
    public static void main( String[] args ){
    	System.out.println("对123456加密");
    	String md5 = getMD5("123456");
    	System.out.println("第一次加密："+md5);
    	System.out.println("第二次加密："+getMD5(md5));
		System.out.println("第三次加密："+getMD5(getMD5(md5)));
    }
    
    public static String getMD5(String password){
    	String psd = "";
    	try {
			MessageDigest digest = MessageDigest.getInstance("md5");
			byte[] result = digest.digest(password.getBytes());
			StringBuffer buffer = new StringBuffer();
			//把每个byte，做一个与运算 0xff
			for(byte b : result){
				//与运算
				int number = b & 0xff;	//加盐:保密，上线必改
				String str = Integer.toHexString(number);
				
//				System.out.println(str);
				if(str.length() == 1){
					buffer.append("0");
				}
				buffer.append(str);
			}
			
			//标准md5加密的结果
			psd = buffer.toString();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return psd;
    }
}
